package haoyou.csnf.manager.module.pm.controller.admin.employeetitles;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.employeetitles.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeetitles.EmployeeTitlesDO;
import haoyou.csnf.manager.module.pm.service.employeetitles.EmployeeTitlesService;

@Tag(name = "管理后台 - 职工职称")
@RestController
@RequestMapping("/pm/employee-titles")
@Validated
public class EmployeeTitlesController {

    @Resource
    private EmployeeTitlesService employeeTitlesService;

    @PostMapping("/create")
    @Operation(summary = "创建职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:create')")
    public CommonResult<Long> createEmployeeTitles(@Valid @RequestBody EmployeeTitlesSaveReqVO createReqVO) {
        return success(employeeTitlesService.createEmployeeTitles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:update')")
    public CommonResult<Boolean> updateEmployeeTitles(@Valid @RequestBody EmployeeTitlesSaveReqVO updateReqVO) {
        employeeTitlesService.updateEmployeeTitles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职工职称")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:delete')")
    public CommonResult<Boolean> deleteEmployeeTitles(@RequestParam("id") Long id) {
        employeeTitlesService.deleteEmployeeTitles(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职工职称")
                @PreAuthorize("@ss.hasPermission('pm:employee-titles:delete')")
    public CommonResult<Boolean> deleteEmployeeTitlesList(@RequestParam("ids") List<Long> ids) {
        employeeTitlesService.deleteEmployeeTitlesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职工职称")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:query')")
    public CommonResult<EmployeeTitlesRespVO> getEmployeeTitles(@RequestParam("id") Long id) {
        EmployeeTitlesDO employeeTitles = employeeTitlesService.getEmployeeTitles(id);
        return success(BeanUtils.toBean(employeeTitles, EmployeeTitlesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职工职称分页")
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:query')")
    public CommonResult<PageResult<EmployeeTitlesRespVO>> getEmployeeTitlesPage(@Valid EmployeeTitlesPageReqVO pageReqVO) {
        PageResult<EmployeeTitlesDO> pageResult = employeeTitlesService.getEmployeeTitlesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeTitlesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职工职称 Excel")
    @PreAuthorize("@ss.hasPermission('pm:employee-titles:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeTitlesExcel(@Valid EmployeeTitlesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeTitlesDO> list = employeeTitlesService.getEmployeeTitlesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职工职称.xls", "数据", EmployeeTitlesRespVO.class,
                        BeanUtils.toBean(list, EmployeeTitlesRespVO.class));
    }

}